var _extends = Object.assign || function(t) {
    for (var e = 1; e < arguments.length; e++) {
        var a, i = arguments[e];
        for (a in i) Object.prototype.hasOwnProperty.call(i, a) && (t[a] = i[a]);
    }
    return t;
}, _tools = require("../../utils/tools");

function _defineProperty(t, e, a) {
    return e in t ? Object.defineProperty(t, e, {
        value: a,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : t[e] = a, t;
}

var g, city, mIcon, mZyz_img, app = getApp(), page = app.page, request = app.util.request, route = app.route;

function toast(t) {
    wx.showToast({
        icon: "none",
        title: t
    });
}

function getLocation() {
    request({
        url: "Location",
        success: function(t) {
            t = t.province;
            var e, a = [], i = [];
            city = [];
            for (var o = 0; o < t.length; o++) {
                e = t[o], i[o] = {
                    id: e.id,
                    name: e.name
                }, e = e.city, city[o] = [];
                for (var s = 0; s < e.length; s++) city[o].push({
                    id: e[s].id,
                    name: e[s].name
                });
            }
            a.push(i), a.push(city[0]), wx.setStorageSync("location", a), wx.setStorageSync("city", city), 
            g.setData({
                location: a
            }), getInfo();
        }
    });
}

function getInfo() {
    request({
        url: "MLawInfo",
        data: {
            user_id: app.userInfo.id || wx.getStorageSync("userInfo").id
        },
        success: function(t) {
            for (var e = g.data.location[0], a = t.goodareas_id.split(","), i = 0; i < e.length; i++) if (e[i].id == t.province_id) {
                for (var o = 0; o < city[i].length; o++) if (city[i][o].id == t.city_id) {
                    g.setData({
                        "location[1]": city[i],
                        addressIndx: [ i, o ]
                    });
                    break;
                }
                break;
            }
            for (i = 0; i < t.good.length; i++) for (o = 0; o < a.length; o++) {
                if (t.good[i].id == a[o]) {
                    t.good[i].checked = !0;
                    break;
                }
                t.good[i].checked = !1;
            }
            app.item = t.good, mIcon = t.img, mZyz_img = t.zyz_img, g.editor && g.editor.setContents({
                html: t.content || ""
            }), g.setData(_extends({}, t), function() {
                g.onShow();
            });
        }
    });
}

page({
    data: {
        src: app.srcRoot,
        data: (0, _tools.getDate)()
    },
    onLoad: function(t) {
        g = this, mZyz_img = mIcon = "", app.lawContent = "";
        var e = wx.getStorageSync("location");
        city = wx.getStorageSync("city"), e && city ? g.setData({
            location: e
        }, function() {
            getInfo();
        }) : getLocation();
    },
    switchChange: function(t) {
        (0, _tools.subscribe)("LAWYER");
        var e = "dh_state";
        switch (t.currentTarget.dataset.i) {
          case "1":
            e = "img_state";
            break;

          case "2":
            e = "jslt_state";
        }
        g.setData(_defineProperty({}, e, t.detail.value ? 2 : 1));
    },
    onShow: function() {
        var t = [];
        if (app.item) {
            for (var e = 0; e < app.item.length; e++) app.item[e].checked && t.push(app.item[e]);
            g.setData({
                goodareas: t
            });
        }
    },
    addressColumnChange: function(t) {
        var e = (t = t.detail).column, t = t.value;
        0 === e && g.setData({
            "location[1]": city[t]
        });
    },
    addressChange: function(t) {
        g.setData({
            addressIndx: t.detail.value
        });
    },
    Change: function(t) {
        var e = t.currentTarget.dataset.i;
        g.setData(_defineProperty({}, e, t.detail.value));
    },
    toDomain: function() {
        route({
            type: "navigate",
            url: "domain"
        });
    },
    ChooseImage: function(t) {
        var e, a = parseInt(t.currentTarget.dataset.type);
        wx.chooseImage({
            count: 1,
            success: function(t) {
                switch (a) {
                  case 0:
                    e = "img";
                    break;

                  case 3:
                    e = "zyz_img";
                }
                g.setData(_defineProperty({}, e, t.tempFilePaths[0]));
            }
        });
    },
    submit: function(t) {
        (0, _tools.subscribe)("LAWYER"), t = t.detail.value;
        for (var e, a, i = g.data, o = t.name, s = t.lawfirm, n = i.work_year, r = (2 == i.dh_state && 1 != g.data.system.is_dianhua ? t : i).dh_price, c = (2 == i.img_state && 1 != g.data.system.is_tuwen ? t : i).img_price, d = (2 == i.jslt_state && 1 != g.data.system.is_jslt ? t : i).jslt_price, u = i.dh_state, l = i.img_state, m = i.jslt_state, p = t.dh_unit || i.dh_unit, _ = t.img_unit || i.img_unit, f = t.jslt_unit || i.jslt_unit, h = app.lawContent || i.content || "", y = t.phone, w = i.img == mIcon ? [ -1 ] : [ i.img ], t = i.zyz_img == mZyz_img ? [] : [ i.zyz_img ], v = "", z = 0; z < i.goodareas.length; z++) v += (0 != z ? "," : "") + i.goodareas[z].id;
        if (e = i.location[0][i.addressIndx[0]].id, a = i.location[1][i.addressIndx[1]].id, 
        o) if (y) if (s) if (i.chooseLocation) if (v) if (r) if (c) if (d) if (w) {
            if ("2" == this.data.system.zyz_state && !t) return toast("请上传" + (this.data.system.zyz_title || "执业证照片"));
            wx.showLoading({
                title: "上传图片中...",
                mask: !0
            }), "2" == this.data.system.zyz_state && i.zyz_img != mZyz_img && w.push(t[0]), 
            (0, _tools.uploadImg)(w, function(t) {
                wx.showLoading({
                    title: "提交中...",
                    mask: !0
                }), t = t.split(","), request({
                    url: "MLawUpdate",
                    data: _extends({
                        user_id: app.userInfo.id || wx.getStorageSync("userInfo").id,
                        name: o,
                        lawfirm: s,
                        dh_price: r,
                        img_price: c,
                        jslt_price: d,
                        dh_state: u,
                        img_state: l,
                        work_year: n,
                        jslt_state: m,
                        goodareas_id: v,
                        province_id: e,
                        city_id: a,
                        content: h,
                        phone: y,
                        img: t[0] || i.img_url,
                        zyz_img: t[1] || i.zyz_img_url,
                        dh_unit: p,
                        img_unit: _,
                        jslt_unit: f
                    }, i.chooseLocation),
                    showLoading: !1,
                    method: "POST",
                    success: function(t) {
                        wx.hideLoading(), app.item = {
                            title: "修改成功",
                            content: "修改提交成功",
                            img: "pay_ok",
                            button: [ {
                                title: "返回首页",
                                url: "l_index",
                                type: "reLaunch"
                            } ]
                        }, route({
                            type: "redirect",
                            url: "result"
                        });
                    }
                });
            });
        } else toast("请上传头像"); else toast("请填写" + g.data.system.jslt_title + "价格"); else toast("请填写" + g.data.system.tuwen_title + "价格"); else toast("请填写" + g.data.system.dianhua_title + "价格"); else toast("请选择擅长领域"); else toast("请选择所在位置"); else toast("请填写" + g.data.system.ssls); else toast("请填手机号码"); else toast("请填写姓名");
    },
    getAddress: function() {
        wx.chooseLocation({
            success: function(t) {
                g.setData({
                    chooseLocation: {
                        address: t.address,
                        longitude: t.longitude,
                        latitude: t.latitude
                    }
                });
            },
            fail: function(t) {
                console.log(t), wx.showModal({
                    content: "请进入设置打开位置授权",
                    title: "提示",
                    success: function(t) {
                        t.confirm && wx.openSetting({
                            success: function(t) {
                                t.authSetting["scope.userLocation"] && g.getAddress();
                            }
                        });
                    }
                });
            }
        });
    },
    toEditor: function() {
        app.lawContent = app.lawContent || this.data.content, route({
            url: "editor"
        });
    }
});